package com.flower8.controller;

import com.flower8.model.Bank;
import com.flower8.model.PointMall;
import com.flower8.service.PointService;
import com.flower8.sysconfig.constants.MsgConstants;
import com.flower8.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: tanglin
 * @Date: 2019/12/31 17:50
 * @Version: 1.0
 */
@RestController
@RequestMapping("/point")
public class pointController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(pointController.class);

    @Autowired
    private PointService pointService;

    /**
     * 查询积分商城商品列表
     * @param request
     * @return
     */
    @RequestMapping("/getPointMallList")
    public Object getPointMallList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询积分商城商品列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<Map> dataList =  pointService.getPointMallList(pd);
            int total = pointService.getPointMallCount(pd);
            res.put("data", dataList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 根据积分商品ID查询
     * @param request
     * @return
     */
    @RequestMapping("/getPointMallById")
    public Object getPointMallById(HttpServletRequest request){
        logBefore(log, "++++++++++ 根据积分商品ID查询 ++++++++++");
        PageData pd = new PageData(request);
        Map res = new HashMap();
        try {
            Map data =  pointService.getPointMallById(Long.parseLong(pd.getString("id")));
            res.put("mall", data);
            logAfter(log);
            return responseResultForSuccess(res);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 更新/新增积分商城商品
     * @param pointMall
     * @return
     */
    @RequestMapping("/updPointMall")
    public Object updPointMall(@RequestBody PointMall pointMall){
        try {
            if(pointMall.getId() != null){
                logBefore(log, "++++++++++ 更新积分商城商品 ++++++++++");
                pointService.updatePointMall(pointMall);
            }else{
                logBefore(log, "++++++++++ 新增积分商城商品 ++++++++++");
                pointService.addPointMall(pointMall);
            }
            logAfter(log);
            return responseResultForSuccess(null, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("操作失败");
        }
    }


    /**
     * 删除积分商城商品
     * @param id
     * @return
     */
    @RequestMapping("/deletePointMall")
    public Object deletePointMall(@RequestParam Long id){
        try {
            logBefore(log, "++++++++++ 删除积分商城商品 ++++++++++");
            pointService.deletePointMall(id);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }

    /**
     * 积分商城兑换记录列表
     * @param request
     * @return
     */
    @RequestMapping("/getMallRecordList")
    public Object getMallRecordList(HttpServletRequest request){
        logBefore(log, "++++++++++ 积分商城兑换记录列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<Map> dataList =  pointService.getMallRecordList(pd);
            int total = pointService.getMallRecordListCount(pd);
            res.put("data", dataList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return res;
    }

    /**
     * 发放兑换商品
     * @param request
     * @return
     */
    @RequestMapping("/sendMallRecord")
    public Object sendMallRecord(HttpServletRequest request){
        logBefore(log, "++++++++++ 发放兑换商品 ++++++++++");
        PageData pd = new PageData(request);
        try {
            pointService.sendMallRecord(pd);
            logAfter(log);
            return returnSuccessMsg(null,"发放成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnFailureMsg("发放失败");
        }
    }

}
